package gui;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.trolltech.qt.gui.*;

import gestionBDD.extractionBDD;
import gestionBDD.requetesContient;
import gestionBDD.requetesImage;
import interne.*;

public class fenetrePhotoPub extends QWidget {
	
	Client client ;
	Album album ;
	Contient contient ;
	QListWidget photosListWidget;	
	QTextBrowser detailsTextDocument ;
	
	public fenetrePhotoPub(Client c, Album a) throws SQLException {
		setClient(c) ;
		setAlbum(a) ;
		
		try {
			photosListWidget = new QListWidget();
			photosListWidget.currentItemChanged.connect(this, "afficherDetails()");
			
			detailsTextDocument = new QTextBrowser();

			/** Login's GroupBox' **/
	        QGroupBox photosGroup = new QGroupBox(tr("Photos contenues dans l'album public"));
	        QHBoxLayout photoslayout = new QHBoxLayout();
	        photoslayout.addWidget(photosListWidget);
	        photoslayout.addWidget(detailsTextDocument);
	        photosGroup.setLayout(photoslayout);	        
	        
	        /* Bouton Retour */
	        QPushButton boutonRetour = new QPushButton("Retour");
	        boutonRetour.clicked.connect(this, "retour()");  
			
	        /** Login's GroupBox' **/
	        QGroupBox boutonsGroup = new QGroupBox(tr("Actions"));
	        QGridLayout boutonLayout = new QGridLayout();
	        boutonLayout.addWidget(boutonRetour, 1, 1);
	        boutonsGroup.setLayout(boutonLayout);
	        
	        QVBoxLayout layout = new QVBoxLayout();
	        layout.addWidget(photosGroup);
	        layout.addWidget(boutonsGroup);
	        setLayout(layout);
	        
			List<Contient> listePhoto = new ArrayList<Contient>(); 
			listePhoto = extractionBDD.extrairePhotos(album);
			Iterator<Contient> ite = listePhoto.iterator();
			while (ite.hasNext()) {
			    Contient photo = ite.next();
			    photosListWidget.addItem(photo.getOrdre()+ " : " + photo.getTitreImage() + " / " + photo.getIdContient()) ;
			}
		}
		catch(SQLException e) {
			System.out.println(e) ;
		}
	}
	
	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}
	
	public void afficherDetails() throws SQLException {
		String idContenu = photosListWidget.currentItem().text() ;
		String temp[] = idContenu.split(" / ");
		idContenu = temp[1] ;
		Contient contient = new Contient(Integer.parseInt(idContenu)) ;
		setContient(requetesContient.trouverAvecId(contient)) ;
		Image image = new Image(contient.getIdImage()); 
		image = requetesImage.trouverAvecId(image);
		
		detailsTextDocument.clear() ;
		detailsTextDocument.setText("Titre Album : " + contient.getIdAlbum()  + "\n" +
				"Ordre : " + contient.getOrdre() + "\n" +
				"Titre Photo : " + contient.getTitreImage() + "\n" +
				"Commentaire : " + contient.getCommentaire() + "\n\n" +
				"Chemin : " + image.getChemin() + "\n" +
				"Appareil Photo : " + image.getAppareilPhoto() + "\n" +
				"Resolution : " + image.getResolution() + "\n" +
				"Objectif : " + image.getObjectif() + "\n" +
				"Distance focale : " + image.getDistFocale() + "\n" +
				"Sensibilite ISO : " + image.getSensibiliteISO() + "\n" +
				"Ouverture : " + image.getOuverture() + "\n" +
				"Vitesse Obturation : " + image.getVitesseObturation()) ; 
	}
	
	public void retour() throws SQLException {
        fenetreAlbumPub fenetre = new fenetreAlbumPub(client);
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}

	public Contient getContient() {
		return contient;
	}

	public void setContient(Contient contient) {
		this.contient = contient;
	}
	
}
